﻿using System;

namespace Common.Extend
{
    public static class ExDateTime
    {
        /// <summary>
        /// 把日期格式化
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>返回格式：yyyy-MM-dd HH:mm:ss</returns>
        public static string ExToFormatString(this DateTime dt)
        {
            return dt.ToString("yyyy-MM-dd HH:mm:ss");
        }
        /// <summary>
        /// 把日期格式化
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>返回格式：yyyy-MM-dd HH:mm</returns>
        public static string ExFormatYYYYMMDDHHMM(this DateTime dt)
        {
            return dt.ToString("yyyy-MM-dd HH:mm");
        }
        /// <summary>
        /// 把日期格式化
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>返回格式：MM-dd HH:mm</returns>
        public static string ExFormatMMDDHHMM(this DateTime dt)
        {
            return dt.ToString("MM-dd HH:mm");
        }
        /// <summary>
        /// 把日期格式化
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>返回格式：yyyy-MM-dd</returns>
        public static string ExFormatYYYYMMDD(this DateTime dt)
        {
            return dt.ToString("yyyy-MM-dd");
        }
        public static string ExToFormatString(this DateTime? dt)
        {
            if (dt.HasValue)
            {
                return dt.Value.ExToFormatString();
            }
            return "";
        }
        /// <summary>
        /// 把日期格式化
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>返回格式：yyyy-MM-dd HH:mm</returns>
        public static string ExFormatYYYYMMDDHHMM(this DateTime? dt)
        {
            if (dt.HasValue)
            {
                return dt.Value.ExFormatYYYYMMDDHHMM();
            }
            return "";
        }
        /// <summary>
        /// 把日期格式化
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>返回格式：MM-dd HH:mm</returns>
        public static string ExFormatMMDDHHMM(this DateTime? dt)
        {
            if (dt.HasValue)
            {
                return dt.Value.ExFormatMMDDHHMM();
            }
            return "";
        }
        /// <summary>
        /// 把日期格式化
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>返回格式：yyyy-MM-dd</returns>
        public static string ExFormatYYYYMMDD(this DateTime? dt)
        {
            if (dt.HasValue)
            {
                return dt.Value.ExFormatYYYYMMDD();
            }
            return "";
        }
        /// <summary>
        /// 返回该时间与endDate的时间间隔
        /// </summary>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <param name="type">时间间隔类型</param>
        /// <returns>返回间隔标志指定的时间间隔</returns>
        public static double ExDiff(this DateTime beginDate, DateTime endDate, TimeSpanType type)
        {
            double dblYearLen = 365.0;
            double dblMonthLen = 30.0;
            TimeSpan ts = endDate.Subtract(beginDate);
            switch (type)
            {
                case TimeSpanType.Y:
                    return (double)Convert.ToInt32(ts.TotalDays / dblYearLen);
                case TimeSpanType.M:
                    return (double)Convert.ToInt32(ts.TotalDays / dblMonthLen);
                case TimeSpanType.d:
                    return ts.TotalDays;
                case TimeSpanType.h:
                    return ts.TotalHours;
                case TimeSpanType.m:
                    return ts.TotalMinutes;
                case TimeSpanType.s:
                    return ts.TotalSeconds;
                case TimeSpanType.ms:
                    return ts.TotalMilliseconds;
                default:
                    return 0.0;
            }
        }
        /// <summary>
        /// 获取两个日期之间的工作日总数
        /// </summary>
        /// <param name="beginDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        public static long ExGetDayOfWeekTotal(this DateTime beginDate, DateTime endDate)
        {
            long countday = (long)endDate.Subtract(beginDate).Days;
            long weekday = 0L;
            int i = -1;
            while ((long)i < countday)
            {
                beginDate.AddDays((double)(i + 1));
                weekday += 1L;
                i++;
            }
            return weekday;
        }
    }
}
